<template>
  <div class="container">
    <div class="app-container">
      <el-tree v-loading="loading" :expand-on-click-node="false" :data="depts" :props="defaultProps" default-expand-all>
        <template v-slot="{data}">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col> {{ data.name }}</el-col>
            <el-col :span="4">
              <span class="depa-G">{{ data.managerName }}</span>
              <!-- command事件  -->
              <el-dropdown @command="operateDept($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">新增子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">修改部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <addDepaVue ref="addDepa" :node-id="nodeId" :dialog-visible.sync="dialogVisible" @updataGx="getDepartment" />
  </div>
</template>
<script>
import { zhuanghua } from '@/utils'
import { getDepartment, remvDepa } from '@/api/department'
import addDepaVue from './components/add_depa'
export default {
  name: 'Department',
  components: {
    addDepaVue
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name', // 显示数据的字段名
        children: 'children'// 找下一级的树形结构默认是children可以不写
      },
      dialogVisible: false,
      nodeId: null,
      loading: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.loading = true
      const res = await getDepartment()
      this.depts = zhuanghua(res, 0)
      this.loading = false
    },
    operateDept(type, id) {
      if (type === 'add') {
        this.nodeId = id
        this.dialogVisible = true
      } else if (type === 'edit') {
        this.nodeId = id
        this.$nextTick(async() => {
          await this.$refs.addDepa.getDepar()
          this.dialogVisible = true
        })
      } else {
        this.$confirm('确认删除此条数据吗?').then(async() => {
          await remvDepa(id)
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
.depa-G{
  margin-right: 10px;
}
</style>
